package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;


public class JDBCDemo1 {
    public static void main(String[] args) {
        try {
            // 正确加载 MySQL 驱动类
            Class.forName("com.mysql.cj.jdbc.Driver");

            // 建立数据库连接
            String url = "jdbc:mysql://localhost:3306/tedu?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true";
            String username = "root";
            String password = "ROOT";

            Connection conn = DriverManager.getConnection(url, username, password);

            // 输出连接成功信息
            System.out.println("数据库连接成功！");

            Statement statement = conn.createStatement();
            String sql="CREATE TABLE user("
                    + "id INT PRIMARY KEY AUTO_INCREMENT,"
                    + "name VARCHAR(20) NOT NULL,"
                    + "password VARCHAR(20) NOT NULL,"
                    + "email VARCHAR(50) NOT NULL,"
                    + "phone VARCHAR(20) NOT NULL,"
                    + "address VARCHAR(100) NOT NULL,"
                    + "regTime DATETIME NOT NULL"
                    + ")";
            statement.execute(sql);
            System.out.println("创建完成");
            statement.close();
        } catch (Exception e) {
            // 捕获并打印异常堆栈信息
            e.printStackTrace();
        }
    }
}


